package com.lill.stock.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.model.P;
import com.lill.common.component.util.IdUtil;
import com.lill.org.service.ICompanyBranchService;
import com.lill.org.service.IPersonService;
import com.lill.product.model.ProductCarModel;
import com.lill.product.service.IProductCarService;
import com.lill.stock.dao.IStockRouteDao;
import com.lill.stock.model.StockRouteModel;
import com.lill.stock.service.IStockRouteService;
import com.lill.system.service.ISysImportService;

/**
 * 车辆在途Service实现类
 *
 * @author lill
 * @date 2021-09-09 11:20:55
 */
@Service("com.lill.stock.service.impl.StockRouteServiceImpl")
public class StockRouteServiceImpl implements IStockRouteService {
	
    @Autowired
    private IStockRouteDao stockRouteDao;
    @Autowired
    private IProductCarService productCarService;
    @Autowired
    private ICompanyBranchService companyBranchService;
    @Autowired
    private IPersonService personService;
    @Autowired
    private ISysImportService sysImportService;

    public StockRouteModel queryObject(String id) {
    	StockRouteModel stockRoute=stockRouteDao.queryObject(id);
    	if(null!=stockRoute&&StringUtils.isNotEmpty(stockRoute.getProductId())){
    		ProductCarModel carVo=productCarService.queryObject(stockRoute.getProductId());
    		if(null!=carVo){
    			stockRoute.setProductName(carVo.getAllName());
    			stockRoute.setProductVo(carVo.getAllName());
    		}
    	}
        return stockRoute;
    }

    public List<StockRouteModel> queryList(Map<String, Object> map) {
    	List<StockRouteModel> stockRouteList=stockRouteDao.queryList(map);
    	if(CollectionUtils.isNotEmpty(stockRouteList)){
    		for (StockRouteModel stockRoute : stockRouteList) {
				ProductCarModel carVo=productCarService.queryObject(stockRoute.getProductId());
				if (carVo!=null){
					stockRoute.setProductVo(carVo);
				}
			}
    	}
        return stockRouteList;
    }

    public int queryTotal(Map<String, Object> map) {
        return stockRouteDao.queryTotal(map);
    }

    public int save(StockRouteModel stockRoute) {
        stockRoute.setId(IdUtil.createIdbyUUID());
        return stockRouteDao.save(stockRoute);
    }

    public int update(StockRouteModel stockRoute) {
        return stockRouteDao.update(stockRoute);
    }

	public int deleteLogicByMap(Map<String, Object> map) {
		return stockRouteDao.deleteLogicByMap(map);
	}

	public List<StockRouteModel> selectStockRouteList(String[] id) {
		Map<String, String> mapInfo=new HashMap<String, String>();
		Map<String, ProductCarModel> mapCar=new HashMap<String, ProductCarModel>();
		List<StockRouteModel> list = stockRouteDao.selectStockRouteList(id);
		if(CollectionUtils.isNotEmpty(list)){
			for (StockRouteModel stockRoute : list) {
				if(mapInfo.containsKey(stockRoute.getProductId())){
					stockRoute.setProductName(mapInfo.get(stockRoute.getProductId()));
					stockRoute.setProductVo(mapCar.get(stockRoute.getProductId()));
				}else{
					ProductCarModel carVo=productCarService.queryObject(stockRoute.getProductId());
		    		if(null!=carVo){
		    			stockRoute.setProductName(carVo.getAllName());
		    			mapInfo.put(stockRoute.getProductId(), carVo.getAllName());
		    			mapCar.put(stockRoute.getProductId(), carVo);
		    		}else{
		    			mapInfo.put(stockRoute.getProductId(), "");
		    			mapCar.put(stockRoute.getProductId(), carVo);
		    		}
				}
			}
		}
		return list;
	}
	
	public P importData(Map<String, Object> baseMap, String booktable, InputStream in, boolean xlsxFormat) {
		P result=P.ok();
		int rowNumStart=0;
		List<Map<String, Object>> importDataList = sysImportService.readDataListFromExcel(rowNumStart,booktable, in, xlsxFormat);
		if(CollectionUtils.isNotEmpty(importDataList)){
			List<Map<String, Object>> importDataUseList=new ArrayList<Map<String, Object>>();
			for (Map<String, Object> mapImport : importDataList) {
				Map<String, Object> useMap=new HashMap<String, Object>();
				if(null!=mapImport.get("code")&&StringUtils.isNotEmpty(mapImport.get("code").toString().trim())){
					useMap.putAll(mapImport);
					useMap.putAll(baseMap);
					useMap.put("status",1);
					useMap.put("top_classify_id",ProductCarModel.classify);
					useMap.put("route_date",new Date());
					useMap.put("id",IdUtil.createIdbyUUID());
					importDataUseList.add(useMap);
				}
			}
			if(!importDataUseList.isEmpty()){
				stockRouteDao.saveBatchRoute(importDataUseList);
			}
		}
		return result;
	}
}
